package org.example.springbootwebdemo.service.impl;

import org.example.springbootwebdemo.dao.UserDao;
import org.example.springbootwebdemo.pojo.User;
import org.example.springbootwebdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Primary
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userdao;
    @Override
    public List<User> findAll() {
        //调用userdao的方法来获取数据,并返回一个List<String>的结果
        List<String> lines = userdao.findAll();
        //2.解析数据-->对象
        //1.????line->这个lambda表达式是怎么写的,用匿名内部类是怎么写的?
        List<User> userList = lines.stream().map(line -> {
            String[] split = line.split(",");
            int id = Integer.parseInt(split[0]);
            String name = split[1];
            String username = split[2];
            String password = split[3];
            //2.?????为什么这里解析成integer?
            Integer age = Integer.parseInt(split[4]);
            LocalDateTime time = LocalDateTime.parse(split[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            User user = new User(id, name, username, password, age, time);
            return user;
        }).toList();
        return userList;
    }
}
